﻿using System;

using System.Linq;
using System.Collections.Generic;

/// <summary>
/// Summary description for Users
/// </summary>
public class Users
{
    public static Common.UserCreateStatus CreateUser(string userName, string password, string email, string phoneNumber)
    {
        // encode username
        userName = Common.Encoding.EncodeString(userName);

        // get stored users
        List<Common.User> Users = DataTier.GetUsers();

        // Validate user doesn't exist already
        if (Users.Count(n => n.userName == userName) != 0)
        {
            return Common.UserCreateStatus.AlreadyExists;
        }

        // Create a new GUID and validate it doesn't exist already
        string newGuid = Common.Utilities.GenerateGUID();
        while (Users.Count(n => n.userId == newGuid) != 0)
        {
            newGuid = Common.Utilities.GenerateGUID();
        }

        // Create a new user and encode it's data
        Common.User user = new Common.User();
        user.userName = userName;
        user.email = Common.Encoding.EncodeString(email);
        user.password = Common.Encoding.EncodeString(password);
        user.userId = newGuid;
        user.phoneNumber = Common.Encoding.EncodeString(phoneNumber);
       
        Users.Add(user);

        // save the user
        DataTier.SaveUsers(Users);

        return Common.UserCreateStatus.Created;
    }

    public static string LogUser(string userName, string password)
    {
        // encode username
        userName = Common.Encoding.EncodeString(userName);
        
        // encode password
        password = Common.Encoding.EncodeString(password);

        // get stored users
        List<Common.User> Users = DataTier.GetUsers();
        // validate user and password and return userId
        var userId = Users.Where(n => n.userName == userName && n.password == password).Select(n => n.userId);
        foreach (string user in userId)
        {
            return user;
        }

        return "Invalid";
    }

    public static Common.User GetUserProfile(string userName)
    {
        userName = Common.Encoding.EncodeString(userName);
        
        // get stored users
        List<Common.User> Users = DataTier.GetUsers();

        // get user passed in parameter
        var userProfile = Users.Where(n => n.userName == userName);
        foreach (Common.User user in userProfile)
        {
            user.password = ""; // so we don't send back any passwords
            return user;
        }

        // return an empty string if no user was found
        return new Common.User();
    }

    public static Common.FollowUserStatus FollowUser(string userId, string userIdToFollow)
    {
        List<Common.User> Users = DataTier.GetUsers();
        foreach (Common.User user in Users)
        {
            if (user.userId == userId)
            {
                if (user.usersFollowed.Contains(userIdToFollow))
                {
                    return Common.FollowUserStatus.AlreadyFollowing;
                }
                else
                {
                    user.usersFollowed.Add(userIdToFollow);
                    break;
                }
            }
        }
        DataTier.SaveUsers(Users);
        return Common.FollowUserStatus.Following;
    }


}
